const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {
      this.setData({
        index: 0,
        password_show: false,
        show_pay: false,
        show_dialog: false,
        pay_index: 0,
        is_has: false
      })
      this.getData();
    },
    hide() {},
    resize() {},
  },
  lifetimes: {
    ready() {
      this.getData()
      this.get_buy_data();
      this._getBalancePwdInfo()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tabbar_active: 0,
    index: 0,
    sex: ["会员", '管理员'],
    type: ['登录密码', '支付密码'],
    radio: 1,
    count: '',
    show_dialog: false,
    show1: false,
    cservice: "",
    service_mobile: "",
    service_QRcode: "",
    active: 0,
    password_open: true,
    password: '',
    show_pay: false,
    pay_index: 0,
    role: "member",
    listData: [],
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    datas: {},
    password_show: false,

    subscribe_title: "",
    activity_id: '',
    active_value: 0,
    require_pwd: false,
    is_admin: false,

    carousel_min_app_link: "",
    banner_min_app_link: "",
    banner_thumb_url: "",
    carousel_imgs_url: [],
    sign_multiple: "",
    activity_list: [],
    service_mobile: "",
    service_QRcode: "",
    Height: 0,
    LastHeight: 0,
    Banner_Height: 0,
    Last_Banner_Height: 0,

    is_has: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    closePop() {
      this.setData({
        show1: false
      })
    },
    skip(e) {
      let type = e.currentTarget.dataset.type;
      if (type == 'carousel') {
        wx.navigateTo({
          url: `${this.data.carousel_min_app_link}`,
        })
      } else {
        wx.navigateTo({
          url: `${this.data.banner_min_app_link}`,
        })
      }
    },
    getKeFu() {
      let urlStr = app.getNetAddresss("goods.customer-service.index");
      app._postNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              cservice: res.data.cservice,
              service_QRcode: res.data.service_QRcode,
              service_mobile: res.data.service_mobile,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    },
    getData() {
      app._getNetWork({
        url: app.getNetAddresss("plugin.sign-buy.frontend.sign-buy-page.home"),
        success: (resdata) => {
          let {
            data,
            result,
            msg
          } = resdata.data;
          if (result == 0) {
            return app.tips(msg)
          }

          let {
            carousel_min_app_link,
            banner_min_app_link,
            banner_thumb_url,
            carousel_imgs_url,
            sign_multiple,
            activity_list
          } = data;
          this.setData({
            carousel_min_app_link,
            banner_min_app_link,
            banner_thumb_url,
            carousel_imgs_url,
            sign_multiple,
            activity_list
          })
          if (carousel_imgs_url && carousel_imgs_url.length == 1) {
            this.setData({
              indicatorDots: false
            })
          }

        }
      })
    },
    sign_subscribe_handle(e) {
      let data = e.detail.item;
      this.setData({
        show_dialog: true,
        datas: data,
        subscribe_title: data.activity_name,
        activity_id: data.stage_id,
        count: "",
        password: "",
      })
    },
    get_buy_data() {
      app._getNetWork({
        url: app.getNetAddresss("plugin.sign-buy.frontend.user-sign.index"),
        success: (resdata) => {
          let {
            result,
            msg,
            data: {
              active_value,
              is_admin,
              require_pwd
            }
          } = resdata.data;
          if (result == 0) return this.$toast(msg);
          this.setData({
            active_value,
            require_pwd,
            is_admin
          })
        }
      })
    },
    finish() {
      this.getData()
    },
    link(e) {
      if (e.currentTarget.dataset.url == 'code') {
        wx.navigateTo({
          url: `/packageF/sign_in_subscribe/${e.currentTarget.dataset.url}/${e.currentTarget.dataset.url}?type=2`,
        })
      } else {
        wx.navigateTo({
          url: `/packageF/sign_in_subscribe/${e.currentTarget.dataset.url}/${e.currentTarget.dataset.url}`,
        })
      }

    },
    handleInput(event) {
      this.setData({
        count: event.detail.value
      });
    },
    handle_password(e) {
      this.setData({
        password: e.detail.value
      })
    },
    sigin_handle(type) {

      let json = {
        stage_id: this.data.activity_id,
        amount: this.data.count,
        admin: this.data.index
      }
      if (this.data.index == 0 && this.data.require_pwd == 1) {
        json.pwd_type = this.data.pay_index == 0 ? 'login' : 'pay';
        json.pwd = this.data.password;
      }


      app._getNetWork({
        url: app.getNetAddresss("plugin.sign-buy.frontend.user-sign.submit"),
        data: json,
        success: (resdata) => {
          let {
            result,
            msg,
            data
          } = resdata.data;

          if (result == 0) {
            if (this.data.index == 0 && this.data.require_pwd == 1 && this.data.pay_index == 1) {
              if (data.password_error == 1) {
                this.setData({
                  password_show: true
                });
              } else {
                app.tips(msg)
              }
            } else {
              app.tips(msg)
            }
            return
          };

          app.tips(msg);
          this.getData()
          this.get_buy_data()
          this.setData({
            show_dialog: false,
            index: 0,
            amount: "",
            password: "",
            show_pay: false
          })

        }
      })
    },
    imgHeight: function (e) {
      var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
      var imgh = e.detail.height; //图片高度
      var imgw = e.detail.width; //图片宽度
      var swiperH = (winWid * imgh) / imgw; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
      if (parseInt(swiperH) > this.data.LastHeight) {
        this.setData({
          Height: swiperH, //设置高度
        });
        this.data.LastHeight = parseInt(swiperH);
      }
    },
    BannerHeight: function (e) {
      var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
      var imgh = e.detail.height; //图片高度
      var imgw = e.detail.width; //图片宽度
      var swiperH = (winWid * imgh) / imgw; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
      if (parseInt(swiperH) > this.data.Last_Banner_Height) {
        this.setData({
          Banner_Height: swiperH, //设置高度
        });
        this.data.Last_Banner_Height = parseInt(swiperH);
      }
    },
    confirm(e) {
      let {
        type
      } = e.currentTarget.dataset;
      if (type == 'subscribe') {
        if (this.data.count <= 0) {
          return app.tips("请输入有效签到认购数量")
        }

        if (this.data.index == 0) {
          if (this.data.require_pwd == 1) {
            this.setData({
              show_pay: true
            })
          } else if (this.data.require_pwd == 0) {
            this.sigin_handle('subscribe')
          }
        } else if (this.data.index == 1) {
          this.sigin_handle('subscribe')
        }
      }
      if (type == 'pay') {
        if (this.data.password == '') {
          return app.tips(`请输入${this.data.pay_index==0?'登录':'支付'}密码`)
        }
        this.sigin_handle('pay');
      }
    },
    closePay() {
      this.setData({
        show_pay: false
      })
    },
    closeDialog() {
      this.setData({
        show_dialog: false
      })
    },
    closePas() {
      this.setData({
        show_pay: false
      })
    },
    choose(e) {
      let {
        i,
        type
      } = e.currentTarget.dataset;
      if (type == 'member') {
        this.setData({
          index: i,
          role: i == 1 ? 'admin' : 'member'
        })
      } else {
        this.setData({
          pay_index: i
        })
      }
    },
    onClickHide() {
      this.setData({
        password_show: false
      });
    },
    _getBalancePwdInfo() {
      let urlStr = app.getNetAddresss("member.balance-password.is-has-password");
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              is_has: res.data.is_has
            });
          } else {
            console.log(res.msg);
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    },
    password_handle(e) {
      let type = e.currentTarget.dataset.type;
      if (type == 'forget') {
        if (this.data.is_has) {
          //修改支付密码
          wx.navigateTo({
            url: "/packageA/member/balance_password/balance_password"
          })
        } else {
          //设置支付密码
          wx.navigateTo({
            url: "/packageA/member/set_balance_password/set_balance_password"
          })
        }

      } else {
        this.setData({
          password_show: false
        });
      }
    },
    clickHome() {
      wx.navigateTo({
        url: '/packageG/index/index',
      })
    },
    clickChat() {
      this.setData({
        show1: true
      })
    }
  }
})